package com.platform.utils;

import com.github.qcloudsms.SmsMultiSender;
import com.github.qcloudsms.SmsMultiSenderResult;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;

/**
 * https://github.com/qcloudsms/qcloudsms_java/blob/master/src/test/java/QcloudSmsTest.java
 *
 * @author lipengjun
 * @date 2017年11月18日 下午13:13:23
 */
public class SmsUtil {

    private static SmsClient client;
    /**
     * 指定模板ID群发
     * 签名参数未提供或者为空时，会使用默认签名发送短信
     *
     * @return
     */
    public static SendSmsResponse crSendSms(String phoneNumber[], String templateId, String[] smsContentParams) {
        SmsUtil.client = getSmsClient();
        for (int i =0;  i < phoneNumber.length; i++){
            phoneNumber[i] = "+86" + phoneNumber[i];
        }
        SendSmsRequest req = SmsUtil.getReq(templateId);
        String[] phoneNumberSet = phoneNumber;
        req.setPhoneNumberSet(phoneNumberSet);
        req.setTemplateId(templateId);
        req.setTemplateParamSet(smsContentParams);
        SendSmsResponse resp  = null;
        try {
            resp = client.SendSms(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return resp;
    }

    /**
     * 指定模板ID单发短信
     * 签名参数未提供或者为空时，会使用默认签名发送短信
     *
     * @return
     */
    public static SendSmsResponse crSendSms(String phoneNumber, String templateId, String[] smsContentParams) {
        SmsUtil.client = getSmsClient();
        SendSmsRequest req = SmsUtil.getReq(templateId);
        String[] phoneNumberSet = {"+86"+phoneNumber};
        req.setPhoneNumberSet(phoneNumberSet);
        req.setTemplateId(templateId);
        req.setTemplateParamSet(smsContentParams);
        SendSmsResponse resp  = null;
        try {
            resp = client.SendSms(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return resp;
    }

    public static void main(String [] args) {
        try{
            String templateId = "987287";   //订单信息模板
            String[] params = {"董啸","18851050925","界首市马铃薯农场"};
            SendSmsResponse resp = SmsUtil.crSendSms(new String[]{"18851050925","15951876824"},templateId,params);

            System.out.println(SendSmsResponse.toJsonString(resp));
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    public  static boolean isStatusOk(SendSmsResponse resp){
        SendStatus[] sendStatuses = resp.getSendStatusSet();
        boolean flag = true;
        for (SendStatus s : sendStatuses){
            if(!s.getCode().equals("Ok")){
                flag = false;
                break;
            }
        }
        return flag;
    }
    public static SmsClient getSmsClient(){
        Credential cred = new Credential("AKIDLWR5SZPzIJ3XliOoxpQjlVyA6k7C1ast", "kDPJwJPDk10i8H9k47uPdZFelb9RR9al");
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        if (client == null){
            client = new SmsClient(cred, "ap-nanjing", clientProfile);
        }
        return client;
    }

    public static SendSmsRequest getReq(String templetId){
        SendSmsRequest  req = new SendSmsRequest();
        req.setSmsSdkAppId("1400531016");           //设置appId
        req.setTemplateId(templetId);
        req.setSignName("笑笑提子采摘园");
        return req;
    }

}
